package com.sds.shop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.sds.fw.dao.AbDao;
import com.sds.shop.vo.OrderProductVO;
import com.sds.shop.vo.OrderVO;
import com.sds.shop.vo.ProductVO;

public class OrderProductDAO extends AbDao {
	private static OrderProductDAO dao = new OrderProductDAO();
	public static OrderProductDAO getInstance(){
		return dao;
	}
	
	public void createOrderProduct(){

	}
	
	public ArrayList<OrderProductVO> getOrderProductList(String key, String value){
		ArrayList<OrderProductVO> list = new ArrayList<OrderProductVO>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			conn = connect();

			StringBuffer query = new StringBuffer();
			query.append("SELECT ord_no, prd_no, qty ");
			query.append("FROM tb_order_product ");
						
			
			if( "ALL".equals( value ) || value == null || value.equals("")){ 
				ps = conn.prepareStatement( query.toString() );				
			} else if (key.equals("ord_no")){
				query.append("WHERE ord_no = ? "); 
				ps = conn.prepareStatement( query.toString() );
				ps.setString(1, value);				 
			} else if(key.equals("prd_no")){
				query.append("WHERE prd_no = ? "); 
				ps = conn.prepareStatement( query.toString() );
				ps.setString(1, value);	
			}

			rs = ps.executeQuery();
			while(rs.next()){
				OrderProductVO gvo = new OrderProductVO();
			    gvo.setOrd_no(rs.getString(1));
			    gvo.setPrd_no(rs.getString(2));
			    gvo.setQty(rs.getInt(3));
			    
				list.add(gvo);
			}
			
			rs.close();
			ps.close();
			conn.close();
			
		}catch(Exception ex){
			System.out.println("getOrderProductList exception : " + ex);
			close(conn, ps, rs);
		} 
		
		return list;
	} // getGoodsList
	
	public ArrayList<OrderProductVO> getOrderProductList(String ordNo){
		ArrayList<OrderProductVO> list = new ArrayList<OrderProductVO>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			conn = connect();

			StringBuffer query = new StringBuffer();
			query.append("SELECT ord_no, prd_no, qty ");
			query.append("FROM tb_order_product WHERE ord_no = ? ");
			ps = conn.prepareStatement(query.toString());
			ps.setString(1, ordNo);
			rs = ps.executeQuery();
			
			OrderProductVO gvo = null;
			while(rs.next()){
				gvo = new OrderProductVO();
			    gvo.setOrd_no(rs.getString(1));
			    gvo.setPrd_no(rs.getString(2));
			    gvo.setQty(rs.getInt(3));
			    
				list.add(gvo);
			}

			close(conn, ps, rs);
		}catch(Exception ex){
			System.out.println("getOrderProductList exception : " + ex);
			close(conn, ps, rs);
		} 
		
		return list;
	} // getGoodsList	

}
